(function(window){
angular.module('feibo.teacher.controller')
         .controller('teacherPackCtrl',teacherPackCtrl);
          teacherPackCtrl.$inject = [ '$timeout','$state', '$stateParams', '$translate', 'UserService', 'TeacherService',
                    'kvMap', 'CommonService'];
        function teacherPackCtrl($timeout, $state, $stateParams, $translate, UserService, TeacherService,
                 kvMap, CommonService){
                 var vm = this;
                        vm.pack = {
                            packList: [],
                            httpGetParams: {
                                search: '',
                                school_id: '',
                                page: 1,
                                page_size: 15
                            },
                            formData: {
                                buyout_teacher_contract_id: '',
                                fb_shift_category_id: '',
                                start_date: '',
                                end_date: '',
                                class_type: '',
                                teacher_type: '',
                                number_of_teachers: '',
                                price: ''
                            },
                            info: {}, 
                            type:{
                              typeList:[]
                            },
                            contract: {
                              contractList: []
                            },
                            classType: {},
                            teacherType:{},
                            handleFn: '',
                            contract_id: '',
                            handleTitle:'',
                            init: function(){
                                       this.classType = CommonService.cloneMapping(kvMap.classType, ['vkMap', 'common']);
                                       this.teacherType = CommonService.cloneMapping(kvMap.teacherType, ['vkMap', 'common']);
                                       return this;
                                 }
                        }.init();
                        vm.initData = function(){
                            vm.fetchTeacherPack();
                            vm.bindJqueryEvent();
                            vm.fetchType();
                            vm.fetchContract();
                        };

                       vm.bindJqueryEvent = function(){
                            $('#startDate').datetimepicker({
                                    yearOffset: 0,
                                    timepicker: false,
                                    format: 'Y-m-d',

                           });
                             $('#endDate').datetimepicker({
                                    yearOffset: 0,
                                    timepicker: false,
                                    format: 'Y-m-d',

                           });
                        };
                      vm.fetchTeacherPack = function(){
                          var queryConfig = {
                              funcName: 'fetchTeacherPack',
                              container: vm.pack,
                              propName: 'packList',
                              propChain: ['data','entries'],
                              showLoadLayer: false,
                              httpGetParams: {
                                   page: vm.pack.httpGetParams.page,
                                   page_size: vm.pack.httpGetParams.page_size,
                              }
                          };
                           TeacherService.fetchTeacherPack(queryConfig).then(function(response){
                                CommonService.paginate(
                                      'paginate-list',
                                      vm.pack.httpGetParams.page,
                                      response.data.pageCount,
                                      function(loadingPageIndex) {
                                          vm.pack.httpGetParams.page = loadingPageIndex;
                                          vm.fetchTeacherPack();
                                      }
                                  )
                          }).catch(function(error){
                              console.error(error);
                          });
                      };
/**************************************************************************************
          获取班级大类
***************************************************************************************/ 
                      vm.fetchType = function(){
                          var queryConfig = {
                              funcName: 'fetchType',
                              container: vm.pack.type,
                              propName: 'typeList',
                              propChain: ['data','entries'],
                              showLoadLayer: false
                          };
                          return TeacherService.fetchClassType(queryConfig).catch(function(error){
                              console.error(error);
                          });
                      };
/**************************************************************************************
          获取老师合同列表
***************************************************************************************/
                      vm.fetchContract = function(){
                            var queryConfig = {
                                funcName: 'fetchContract',
                                container: vm.pack.contract,
                                propName: 'contractList',
                                propChain: ['data','entries'],
                                showLoadLayer: false
                            };
                            return TeacherService.fetchTeacherContract(queryConfig).catch(function(error){
                                console.error(error);
                            });
                      };

/**************************************************************************************
          添加老师合同群
***************************************************************************************/
                      vm.addPack = function(){
                            if (vm.pack.handleFn == 'addPack'){
                                  var postConfig = {
                                        funcName: 'addPack',
                                        container: vm.pack,
                                        propName: '',
                                        successMsg: '添加成功',
                                         errorMsg: '添加失败',
                                        propChain: ['data'],
                                        httpPostData: {}
                                    };
                                  Object.keys(vm.pack.formData).forEach(function(item){
                                      postConfig.httpPostData[item] = vm.pack.formData[item];
                                  });
                                  postConfig.httpPostData.price *= 100;
                                return TeacherService.addTeacherPack(postConfig).then(function(data){
                                      vm.fetchTeacherPack();
                                      $('#addTeacherpack').modal('hide');
                                }).catch(function(err){
                                  console.log(err);
                                });
                            } else if (vm.pack.handleFn == 'update') {
                                var putConfig = {
                                         funcName: 'updatePackInfo',
                                          container: vm.pack.info,
                                          propName: '',
                                          propChain: ['data'],
                                          successMsg: '更新成功',
                                           errorMsg: '跟新失败',
                                          httpPutData: {},
                                          urlPlaceholder:{
                                            id: vm.pack.contract_id
                                          }
                                   };
                                   Object.keys(vm.pack.formData).forEach(function(item){
                                        putConfig.httpPutData[item] = vm.pack.formData[item];
                                    });
                                TeacherService.updatePack(putConfig).then(function(data){
                                    $('#addTeacherpack').modal('hide');
                                      vm.fetchTeacherPack();
                                }).catch(function(err){
                                    console.log(err);
                                })
                            } 
                            
                      };
                      vm.modal = function(handle, info){
                         if (handle =='add'){
                             vm.pack.handleTitle = "添加";
                             vm.pack.handleFn = 'addPack';
                             Object.keys(vm.pack.formData).forEach(function(item){
                                  vm.pack.formData[item] = '';
                              });
                         } 
                         if (handle == 'edit'){
                             vm.pack.handleFn = 'update';
                             vm.pack.handleTitle = "更新";
                             vm.pack.contract_id = info.id;
                                vm.pack.formData.buyout_teacher_contract_id = info.buyout_teacher_contract_id,
                                vm.pack.formData.fb_shift_category_id = info.fb_shift_category_id,
                                vm.pack.formData.start_date = info.start_date,
                                vm.pack.formData.end_date = info.end_date,
                                vm.pack.formData.class_type = info.class_type,
                                vm.pack.formData.teacher_type = info.teacher_type,
                                vm.pack.formData.number_of_teachers = info.number_of_teachers,
                                vm.pack.formData.price = info.price;
                         }
                         $timeout(function(){
                              $('#contract_select').dropdown('set text', '').dropdown('set selected', info&&info.buyout_teacher_contract_id || '');
                              $('#type_select').dropdown('set text', '').dropdown('set selected', info&&info.fb_shift_category_id || '');
                              $('#class_type_select').dropdown('set text', '').dropdown('set selected', info&&info.class_type || '');
                              $('#teacher_type_select').dropdown('set text', '').dropdown('set selected', info&&info.teacher_type || '');
                         }, 0);
                      }
/**************************************************************************************
          获取单个专属老师群信息
***************************************************************************************/
                      //  vm.packInfo = function(id){
                      //       var queryConfig = {
                      //           funcName: 'packInfo',
                      //           container: vm.pack,
                      //           propName: 'info',
                      //           propChain: ['data'],
                      //           showLoadLayer: false,
                      //           urlPlaceholder: {
                      //             id : id
                      //           }
                      //       };
                      //     return TeacherService.getPackInfo(queryConfig).then(function(response){
                      //          vm.modal('edit');
                      //     }).catch(function(error){
                      //         console.error(error);
                      //     });
                      // };
                     
/**************************************************************************************
          删除----单个---专---属---老师群
***************************************************************************************/
                          vm.delpack = function(id){
                                   var deleteConfig = {
                                           funcName: 'delpack',
                                            container: vm.pack,
                                            propName: '',
                                            propChain: ['data'],
                                            successMsg: '删除成功',
                                             errorMsg: '删除失败',
                                            urlPlaceholder:{
                                              id: id
                                            }
                                     };
                                      layer.confirm('您确认要删除吗？', {
                                          btn: ['确定', '取消'] //按钮
                                      }, function (index) {
                                          TeacherService.deletePack(deleteConfig).then(function(data){
                                              vm.fetchTeacherPack();
                                        }).catch(function(err){
                                            console.log(err);
                                        })
                                      });
                                  
                              };

// 表单验证
                  $('#addTeacherpackForm').form({
                        fields: {
                              buyout_teacher_contract_id: {
                                    identifier: 'buyout_teacher_contract_id',
                                    rules: [
                                        {
                                            type   : 'empty',
                                            prompt : '请选择所属合同号!'
                                        }
                                    ]
                              },
                              fb_shift_category_id: {
                                    identifier: 'fb_shift_category_id',
                                    rules: [
                                        {
                                            type   : 'empty',
                                            prompt : '请选择班型大类'
                                        }
                                    ]
                              },
                              start_date: {
                                    identifier: 'start_date',
                                    rules: [
                                        {
                                            type   : 'empty',
                                            prompt : '开始日期不能为空!'
                                        }
                                    ]
                              },
                              end_date: {
                                    identifier: 'end_date',
                                    rules: [
                                        {
                                            type   : 'empty',
                                            prompt : '结束日期不能为空!'
                                        }
                                    ]
                              },
                              price: {
                                    identifier: 'price',
                                    rules: [
                                        {
                                            type   : 'empty',
                                            prompt : '请填写单节课金额!'
                                        }
                                    ]
                              },
                              number_teacher: {
                                    identifier: 'number_teacher',
                                    rules: [
                                        {
                                            type   : 'empty',
                                            prompt : '请填写老师数量!'
                                        }
                                    ]
                             },
                             teacher_type: {
                                    identifier: 'teacher_type',
                                    rules: [
                                        {
                                            type   : 'empty',
                                            prompt : '请选择老师类型!'
                                        }
                                    ]
                             },
                             class_type: {
                                    identifier: 'class_type',
                                    rules: [
                                        {
                                            type   : 'empty',
                                            prompt : '请选择班课类型!'
                                        }
                                    ]
                             },
                           },
                           onSuccess: function(){
                                vm.addPack();
                                return false;
                            },
                            onFailure: function(){
                                return false;
                            }
                 });
                    


      };

})(this);